//
//  XVCVisitorMessage.m 观众消息
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/4/13.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "XVCVisitorMessage.h"
#import "XXTool.h"
#import "XTCVisitorMessage.h"

#import "XVCVisitorAppointmentDetail.h"
#import "XVCRefuseReason.h"
#import "XVCVistorAppointmentRecord.h"
#import "VisitorMessageLiuyanCell.h"
#import "LiuyanViewController.h"

@interface XVCVisitorMessage ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableview;

@property (nonatomic,strong) UIView *navigationTopView;

@property(nonatomic,strong) UIButton *message;

@property(nonatomic,strong) UIButton *YuyueMessage;

@property(nonatomic,strong) CALayer * Liuyantemp;

@property(nonatomic,strong) CALayer * temp;

@property(nonatomic) Boolean isYuyueMessage;

@end

@implementation XVCVisitorMessage
{
    NSMutableArray <XMLVisitorMessageModel*> *useData;
    UIImage* userSelectedHead;
}

-(UIView*)navigationTopView{
    
    
    
    if(_navigationTopView == nil){
        _navigationTopView = [[UIView alloc] initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width-140)/2
                                                                      , (44-30)/2
                                                                      , 140
                                                                      , 30)];
        
        _message=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, _navigationTopView.frame.size.width/2, _navigationTopView.frame.size.height)];
        _Liuyantemp = [CALayer layer];
        _Liuyantemp.frame = CGRectMake(1, 1, _message.bounds.size.width - 1, _message.bounds.size.height-2);
        [_Liuyantemp setBackgroundColor:[XXTool colorWithProject].CGColor];
        
        UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:_Liuyantemp.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerBottomLeft cornerRadii:CGSizeMake(5.5, 5.5)];
        
        CAShapeLayer * mask  = [[CAShapeLayer alloc] initWithLayer:_Liuyantemp];
        mask.path = path.CGPath;
        _Liuyantemp.mask = mask;
        [_message.layer addSublayer:_Liuyantemp];
        
        UIBezierPath * path2 = [UIBezierPath bezierPathWithRoundedRect:_message.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerBottomLeft cornerRadii:CGSizeMake(6.0, 6.0)];
        CAShapeLayer * mask2  = [[CAShapeLayer alloc] initWithLayer:_Liuyantemp];
        mask2.path = path2.CGPath;
        _message.layer.mask = mask2;
        _message.backgroundColor=[XXTool colorWithProject];
        [_message setTitle:NSLocalizedString(@"消息", nil) forState:UIControlStateNormal];
        [_message setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _message.titleLabel.font = [XXTool systemFontOfSize:15];
        [_message addTarget:self action:@selector(clickMessage) forControlEvents:UIControlEventTouchDown];
        [_navigationTopView addSubview:_message];
        
        
        _YuyueMessage=[[UIButton alloc]initWithFrame:CGRectMake(_message.frame.size.width, 0, _navigationTopView.frame.size.width/2, _navigationTopView.frame.size.height)];
        
        _temp = [CALayer layer];
                _temp.frame = CGRectMake(0, 1, _YuyueMessage.bounds.size.width - 1, _YuyueMessage.bounds.size.height-2);
                [_temp setBackgroundColor:[UIColor whiteColor].CGColor];
        
                UIBezierPath * path1 = [UIBezierPath bezierPathWithRoundedRect:_temp.bounds byRoundingCorners:UIRectCornerTopRight|UIRectCornerBottomRight cornerRadii:CGSizeMake(5.5, 5.5)];
        
                CAShapeLayer * mask1  = [[CAShapeLayer alloc] initWithLayer:_temp];
                mask1.path = path1.CGPath;
                _temp.mask = mask1;
                [_YuyueMessage.layer addSublayer:_temp];
        
                UIBezierPath * path3 = [UIBezierPath bezierPathWithRoundedRect:_YuyueMessage.bounds byRoundingCorners:UIRectCornerTopRight|UIRectCornerBottomRight cornerRadii:CGSizeMake(6.0, 6.0)];
                CAShapeLayer * mask3  = [[CAShapeLayer alloc] initWithLayer:_temp];
                mask3.path = path3.CGPath;
                _YuyueMessage.layer.mask = mask3;
        
        _YuyueMessage.backgroundColor=[XXTool colorWithProject];
        [_YuyueMessage setTitle:NSLocalizedString(@"预约", nil) forState:UIControlStateNormal];
        [_YuyueMessage setTitleColor:[XXTool colorWithProject] forState:UIControlStateNormal];
        _YuyueMessage.titleLabel.font=[XXTool systemFontOfSize:15];
        [_YuyueMessage addTarget:self action:@selector(clickYuyueMessage) forControlEvents:UIControlEventTouchDown];
        [_navigationTopView addSubview:_YuyueMessage];
    }
    return  _navigationTopView;
}

-(UITableView*)tableview
{
    if (_tableview==nil)
    {
        _tableview = [[UITableView alloc] init];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.backgroundColor = [XXTool colorWithBackGround];
        MJRefreshNormalHeader *useHead = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getData)];
        [useHead setTitle:NSLocalizedString(@"下拉刷新", nil) forState:MJRefreshStateIdle];
        [useHead setTitle:NSLocalizedString(@"刷新中...", nil) forState:MJRefreshStateRefreshing];
        [useHead setTitle:NSLocalizedString(@"松开立即刷新", nil) forState:MJRefreshStatePulling];
        _tableview.mj_header = useHead;
        
        MJRefreshBackNormalFooter *useFooter = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerFresh)];
        [useFooter setTitle:NSLocalizedString(@"刷新中...", nil) forState:MJRefreshStateRefreshing];
        [useFooter setTitle:NSLocalizedString(@"上拉刷新", nil) forState:MJRefreshStateIdle];
        [useFooter setTitle:NSLocalizedString(@"松开立即刷新", nil) forState:MJRefreshStatePulling];
        _tableview.mj_footer = useFooter;
        [self.view addSubview:_tableview];
    }
    return _tableview;
}

-(void)initParameter
{
    useData = [[NSMutableArray alloc] init];
}

-(void)initUI
{
    //[self.navigationItem setTitle:NSLocalizedString(@"消息", nil)];
    [self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"更多"] style:UIBarButtonItemStylePlain target:self action:@selector(clickleft)]];
    
//    [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"预约记录", nil) style:UIBarButtonItemStylePlain target:self action:@selector(clickApponintmentRecord)]];
    [self.navigationItem setTitleView:self.navigationTopView];
    
//    if ([UserInfo shareUserInfo].isExhibitor){
//        _isGuan=false;
//    }else{
//        _isGuan=true;
//    }
    _isYuyueMessage=false;
    [self tableview];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clickCellLiuyan:) name:@"GuanLiuyan" object:nil];
    
}

-(void)clickCellLiuyan:(NSNotification *)notification{
    
    NSString *tagg=[NSString stringWithFormat:@"%d",[[notification.userInfo objectForKey:@"tag"] integerValue]];
    [LiuyanViewController pushVistorLiuyanDetailWithSourceVC:self apmentID:tagg Type:XVCVisitor];
    
}

-(void)initData
{
    [self.view startLoading];
    [self getData];
}

-(void)refreshLayout:(CGRect)frame
{
    self.tableview.frame = CGRectMake(0, 0, frame.size.width
                                      , frame.size.height);
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [_tableview.mj_header beginRefreshing];
}

-(void)getData
{
    if(_isYuyueMessage){
        [XMLVisitorMessageModel requestVisitorMessageWithAcceptType:@"0"
                                                          listCount:0
                                                         completion:^(BOOL isSuccess, NSMutableArray<XMLVisitorMessageModel *> *datalist) {
                                                             if (isSuccess)
                                                             {
                                                                 useData = datalist;
                                                                 [_tableview reloadData];
                                                             }
                                                             [_tableview.mj_header endRefreshing];
                                                             [self.view stopLoading];
                                                         }];
    }else{
        [XMLVisitorMessageModel requestVisitorLiuyanMessageWithType:0 completion:^(BOOL isSuccess, NSMutableArray<XMLVisitorMessageModel *> *datalist) {
            if (isSuccess)
            {
                useData = datalist;
                [_tableview reloadData];
            }
            [_tableview.mj_header endRefreshing];
            [self.view stopLoading];
            
        }];
    }
    
}

-(void)footerFresh
{
    if(_isYuyueMessage){
        [XMLVisitorMessageModel requestVisitorMessageWithAcceptType:@"0"
                                                          listCount:useData.count
                                                         completion:^(BOOL isSuccess, NSMutableArray<XMLVisitorMessageModel *> *datalist) {
                                                             if (isSuccess)
                                                             {
                                                                 if (datalist.count==0)
                                                                 {
                                                                     [SVProgressHUD showInfoWithStatus:NSLocalizedString(@"暂无更多消息", nil)];
                                                                 }
                                                                 else
                                                                 {
                                                                     [useData addObjectsFromArray:datalist];
                                                                     [_tableview reloadData];
                                                                 }
                                                             }
                                                             [_tableview.mj_footer endRefreshing];
                                                         }];
    }else{
        [XMLVisitorMessageModel requestVisitorLiuyanMessageWithType:useData.count completion:^(BOOL isSuccess, NSMutableArray<XMLVisitorMessageModel *> *datalist){
            if (isSuccess)
            {
                if (datalist.count==0)
                {
                    [SVProgressHUD showInfoWithStatus:NSLocalizedString(@"暂无更多消息", nil)];
                }
                else
                {
                    [useData addObjectsFromArray:datalist];
                    [_tableview reloadData];
                }
            }
            [_tableview.mj_footer endRefreshing];
        }];
    }
    
}

/**
 * 点击 显示 留言
 */
-(void)clickMessage{
    _isYuyueMessage=false;
    [self changeStyle];
    [self getData];
}

/**
 *  点击 显示 预约
 */
-(void)clickYuyueMessage{
    _isYuyueMessage=true;
    [self changeStyle];
    [self getData];
}

-(void)changeStyle{
    if(_isYuyueMessage){
        
        [_Liuyantemp setBackgroundColor:[UIColor whiteColor].CGColor];
        _message.backgroundColor=[XXTool colorWithProject];
        [_message setTitleColor:[XXTool colorWithProject] forState:UIControlStateNormal];
        // 预约
         [_temp setBackgroundColor:[XXTool colorWithProject].CGColor];
        _YuyueMessage.backgroundColor=[XXTool colorWithProject];
        [_YuyueMessage setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }else{
        [_Liuyantemp setBackgroundColor:[XXTool colorWithProject].CGColor];
        _message.backgroundColor=[XXTool colorWithProject];
        [_message setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        [_temp setBackgroundColor:[UIColor whiteColor].CGColor];
        _YuyueMessage.backgroundColor=[XXTool colorWithProject];
        [_YuyueMessage setTitleColor:[XXTool colorWithProject] forState:UIControlStateNormal];
    }
}

#pragma mark tableviewDatasource
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(!_isYuyueMessage){
        VisitorMessageLiuyanCell *cell = [VisitorMessageLiuyanCell cellSelectionStyleNoneWithTableView:tableView];
        
        [cell setCellWithData:useData[indexPath.row]];
        return cell;
    }else{
        XTCVisitorMessage *cell = [XTCVisitorMessage cellSelectionStyleNoneWithTableView:tableView];
                [cell setCellWithData:useData[indexPath.row]];
                return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   // NSInteger in=[XTCVisitorMessage getHeightWithData:useData[indexPath.row]];
    return 117;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return useData.count;
}

#pragma mark tableviewdelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    XMLVisitorMessageModel *temp = useData[indexPath.row];
    if(!_isYuyueMessage){  // 留言
        [XVCVisitorAppointmentDetail pushVistorLiuyanDetailWithSourceVC:self apmentID:temp.orderid useType:XVCVisitorLiuyan];
        return;
    }
    
    if(_isYuyueMessage){
        if ([temp.accept isEqualToString:@"0"]) //待接受
        {
            [XVCVisitorAppointmentDetail pushVistorAppointmentDetailWithSourceVC:self apmentID:temp.orderid];
        }
        else if ([temp.accept isEqualToString:@"2"]) //拒绝
        {
            [XVCRefuseReason pushRefuseReasonVCWithSourceVC:self apmentID:temp.orderid];
        }
        else if ([temp.accept isEqualToString:@"3"]) //已逾期
        {
            [XVCVisitorAppointmentDetail pushVistorAppointmentDetailWithSourceVC:self apmentID:temp.orderid useType:XVCVisitorAppointmentDetailDelete];
        }
    }
    
}

-(void)clickleft
{
    //[self.navigationController.sideViewController showLeftView];
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)clickApponintmentRecord
{
    [XVCVistorAppointmentRecord pushVisitorAppointmentRecordVCWithSourceVC:self];
}

@end
